//
//  PropertyViewController.m
//  cm
//
//  Created by suyibaishan on 2017/11/5.
//  Copyright © 2017年 feihu. All rights reserved.
//

#import "PropertyViewController.h"
#import "WithdrawViewController.h"
#import "BillViewController.h"

@interface PropertyViewController () <UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSDictionary *dataSource;
    UIView *headView;
    UILabel *priceLabel;
    UILabel *endLabel;
    UILabel *fundLabel;
    UILabel *carfundLabel;
}
@end

@implementation PropertyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createPropertyView];
}

-(void)reloadData
{
    [[ToolNet sharedManager] getUserInfo:[mUserDefaults objectForKey:@"userCode"] accessToken:[mUserDefaults objectForKey:@"accessToken"] url:MUserCash completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            dataSource = responseJson[@"data"];
            priceLabel.text = [NSString stringWithFormat:@"%.2f",[dataSource[@"availableCash"] floatValue]];
            endLabel.text = [NSString stringWithFormat:@"已提现金额（元）%.2f",[dataSource[@"handCash"] floatValue]];
            fundLabel.text = [NSString stringWithFormat:@"%.2f",[dataSource[@"cash"] floatValue]];
            carfundLabel.text = [NSString stringWithFormat:@"%.2f",[dataSource[@"carFund"] floatValue]];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
    }];
}

-(void)createPropertyView
{
//    self.titleLabel.text = @"个人资产";
//    self.backButton.hidden = NO;

    [self createHeadView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 225, kScreenWidth, kScreenHeight - 225) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

-(void)createHeadView
{
    headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 225)];
    headView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:headView];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 175)];
//    imageView.contentMode = UIViewContentModeScaleAspectFill;
//    imageView.clipsToBounds = YES;
    imageView.image = [UIImage imageNamed:@"cm_ground"];
    [headView addSubview:imageView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, statusBarRect.size.height, 40, 44);
    [backBtn setImage:[UIImage imageNamed:@"back_button"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:backBtn];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    titleLabel.text = @"个人资产";
    titleLabel.textColor = [UIColor whiteColor];
    [headView addSubview:titleLabel];
    titleLabel.sd_layout
    .topSpaceToView(headView, statusBarRect.size.height)
    .leftSpaceToView(headView, 60)
    .widthIs(kScreenWidth - 120)
    .heightIs(44);
    
    UILabel *approveLabel = [[UILabel alloc] init];
    approveLabel.textAlignment = NSTextAlignmentCenter;
    approveLabel.text = @"可提现总金额（元）";
    approveLabel.font = [UIFont systemFontOfSize:12];
    approveLabel.textColor = [UIColor whiteColor];
    [headView addSubview:approveLabel];
    approveLabel.sd_layout
    .topSpaceToView(titleLabel, 5)
    .leftSpaceToView(headView, 50)
    .widthIs(kScreenWidth - 100)
    .heightIs(20);
    
    priceLabel = [[UILabel alloc] init];
    priceLabel.textAlignment = NSTextAlignmentCenter;
    priceLabel.font = [UIFont systemFontOfSize:40];
    priceLabel.textColor = [UIColor whiteColor];
    [headView addSubview:priceLabel];
    priceLabel.sd_layout
    .topSpaceToView(approveLabel, 5)
    .leftSpaceToView(headView, 15)
    .widthIs(kScreenWidth - 30)
    .heightIs(45);
    
    endLabel = [[UILabel alloc] init];
    endLabel.textAlignment = NSTextAlignmentCenter;
    endLabel.textAlignment = NSTextAlignmentCenter;
    endLabel.font = [UIFont systemFontOfSize:13];
//    endLabel.text = [NSString stringWithFormat:@"已提现金额（元）4000.00"];
    endLabel.textColor = [UIColor whiteColor];
    [headView addSubview:endLabel];
    endLabel.sd_layout
    .topSpaceToView(priceLabel, 5)
    .leftSpaceToView(headView, 50)
    .widthIs(kScreenWidth - 100)
    .heightIs(20);
    
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor colorWithRed:0.35 green:0.62 blue:0.99 alpha:1];
    [headView addSubview:bottomView];
    bottomView.sd_layout
    .bottomSpaceToView(headView, 0)
    .leftSpaceToView(headView, 0)
    .widthIs(kScreenWidth)
    .heightIs(50);
    
    fundLabel = [[UILabel alloc] init];
    fundLabel.textAlignment = NSTextAlignmentCenter;
    fundLabel.font = [UIFont systemFontOfSize:13];
//    fundLabel.text = [NSString stringWithFormat:@"4000.00"];
    fundLabel.textColor = [UIColor whiteColor];
    [bottomView addSubview:fundLabel];
    fundLabel.sd_layout
    .leftSpaceToView(bottomView, 0)
    .topSpaceToView(bottomView, 5)
    .widthIs(kScreenWidth/2)
    .heightIs(20);
    
    carfundLabel = [[UILabel alloc] init];
    carfundLabel.textAlignment = NSTextAlignmentCenter;
    carfundLabel.font = [UIFont systemFontOfSize:13];
//    carfundLabel.text = [NSString stringWithFormat:@"4000.00"];
    carfundLabel.textColor = [UIColor whiteColor];
    [bottomView addSubview:carfundLabel];
    carfundLabel.sd_layout
    .leftSpaceToView(fundLabel, 0)
    .topSpaceToView(bottomView, 5)
    .widthIs(kScreenWidth/2)
    .heightIs(20);
    
    for (NSInteger i = 0; i < 2; i++) {
        UILabel *approveLabel = [[UILabel alloc] init];
        approveLabel.textAlignment = NSTextAlignmentCenter;
        approveLabel.text = i == 0?@"可提现现金（元）":@"可提现购车基金（元）";
        approveLabel.font = [UIFont systemFontOfSize:13];
        approveLabel.textColor = [UIColor whiteColor];
        [bottomView addSubview:approveLabel];
        approveLabel.sd_layout
        .bottomSpaceToView(bottomView, 0)
        .leftSpaceToView(bottomView, i*kScreenWidth/2)
        .widthIs(kScreenWidth/2)
        .heightIs(30);
    }
}

-(void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:15];

    NSArray *titleArr = @[@"申请提现",@"查看流水"];
    NSArray * imageArr = @[@"qianbao",@"zoom"];
    cell.textLabel.text = titleArr[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:imageArr[indexPath.row]];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor =  [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [cell.contentView addSubview:line];
    line.sd_layout
    .bottomSpaceToView(cell.contentView, 0)
    .leftSpaceToView(cell.contentView, 0)
    .widthIs(kScreenWidth)
    .heightIs(1);
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        WithdrawViewController *withdrawVC = [[WithdrawViewController alloc] init];
        [self.navigationController pushViewController:withdrawVC animated:YES];
    }
    else {
        BillViewController *billVC = [[BillViewController alloc] init];
        [self.navigationController pushViewController:billVC animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
